<template>
	<view class="home-container">		
		<!-- 未登录状态 -->
		<view class="not-login" v-if="!isLogin">
			<view class="welcome-card">
				<text class="welcome-title">欢迎来到接单平台</text>
				<text class="welcome-subtitle">专业技术服务平台</text>
				<button class="login-btn" @click="goLogin">立即登录</button>
			</view>
		</view>
		<!-- 用户信息卡片 -->
		<view class="user-card" v-if="userInfo">
			<view class="user-info">
				<view class="avatar">
					<text class="avatar-text">{{ userInfo.realName ? userInfo.realName.charAt(0) : 'U' }}</text>
				</view>
				<view class="info">
					<text class="name">{{ userInfo.realName || userInfo.username }}</text>
					<text class="role">{{ $store.getters.roleName }}</text>
				</view>
			</view>
			<view class="stats" v-if="stats">
				<view class="stat-item">
					<text class="number">{{ stats.totalOrders || 0 }}</text>
					<text class="label">总订单</text>
				</view>
				<view class="stat-item">
					<text class="number">{{ stats.processingOrders || 0 }}</text>
					<text class="label">进行中</text>
				</view>
				<view class="stat-item">
					<text class="number">{{ stats.completedOrders || 0 }}</text>
					<text class="label">已完成</text>
				</view>
			</view>
		</view>
		
		<!-- 快捷操作 -->
		<view class="quick-actions">
			<view class="section-title">快捷操作</view>
			<view class="action-grid">
				<view class="action-item" @click="goCreateOrder" v-if="$store.getters.isUser">
					<view class="action-icon create">
						<text class="iconfont">+</text>
					</view>
					<text class="action-text">发布需求</text>
				</view>
				
				<view class="action-item" @click="goOrderList">
					<view class="action-icon orders">
						<text class="iconfont">📋</text>
					</view>
					<text class="action-text">我的订单</text>
				</view>
				
				<view class="action-item" @click="goProfile">
					<view class="action-icon profile">
						<text class="iconfont">👤</text>
					</view>
					<text class="action-text">个人中心</text>
				</view>
				
				<view class="action-item" @click="goEarnings" v-if="$store.getters.isTech">
					<view class="action-icon earnings">
						<text class="iconfont">💰</text>
					</view>
					<text class="action-text">收入明细</text>
				</view>
			</view>
		</view>
		
		<!-- 最新订单 -->
		<view class="recent-orders">
			<view class="section-title">
				<text>最新订单</text>
				<text class="more" @click="goOrderList">查看更多</text>
			</view>
			<view class="order-list">
				<view class="order-item" v-for="order in recentOrders" :key="order.id" @click="goOrderDetail(order.id)">
					<view class="order-header">
						<text class="order-title">{{ order.title }}</text>
						<text class="order-status" :class="getStatusClass(order.status)">{{ getStatusText(order.status) }}</text>
					</view>
					<view class="order-info">
						<text class="budget">￥{{ order.budget }}</text>
						<text class="category">{{ order.categoryName }}</text>
					</view>
					<view class="order-time">{{ formatTime(order.createTime) }}</view>
				</view>
				
				<view class="empty" v-if="recentOrders.length === 0">
					<text>暂无订单数据</text>
				</view>
			</view>
		</view>
		
		<!-- 角色专属入口 -->
		<view class="role-entrance" v-if="showRoleEntrance">
			<view class="section-title">专属功能</view>
			<view class="entrance-item" @click="goRoleDashboard">
				<view class="entrance-icon">
					<text class="iconfont">⚡</text>
				</view>
				<view class="entrance-info">
					<text class="entrance-title">{{ roleEntranceTitle }}</text>
					<text class="entrance-desc">{{ roleEntranceDesc }}</text>
				</view>
				<text class="entrance-arrow">></text>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				stats: null,
				recentOrders: [],
				loading: false,
				isLogin: false // 添加本地状态
			}
		},
		computed: {
			userInfo() {
				return this.$store && this.$store.getters ? this.$store.getters.userInfo : null
			},
			showRoleEntrance() {
				return this.$store.getters.isAdmin || this.$store.getters.isDispatcher || this.$store.getters.isTech
			},
			roleEntranceTitle() {
				if (this.$store.getters.isAdmin) return '管理后台'
				if (this.$store.getters.isDispatcher) return '派单中心'
				if (this.$store.getters.isTech) return '技术员工作台'
				return ''
			},
			roleEntranceDesc() {
				if (this.$store.getters.isAdmin) return '用户管理、系统设置'
				if (this.$store.getters.isDispatcher) return '订单指派、进度跟踪'
				if (this.$store.getters.isTech) return '接单、完成订单'
				return ''
			}
		},
		onLoad() {
			this.checkLogin()
		},
		onShow() {
			if (this.$store.getters.isLogin) {
				this.loadData()
			}
		},
		methods: {
			checkLogin() {
				if (this.$store && this.$store.getters && this.$store.getters.isLogin) {
					this.isLogin = true
					this.loadData()
				} else {
					this.isLogin = false
				}
			},
			
			async loadData() {
				this.loading = true
				try {
					// 调用真实API接口
					const statsRes = await this.$api.getUserStats()
					if (statsRes.code === 200) {
						this.stats = statsRes.data
					}
					
					const ordersRes = await this.$api.getOrderList({ pageSize: 5 })
					if (ordersRes.code === 200) {
						this.recentOrders = ordersRes.data
					}
				} catch (error) {
					console.error('加载数据失败:', error)
					// 加载失败时显示空数据
					this.stats = {
						totalOrders: 0,
						processingOrders: 0,
						completedOrders: 0
					}
					this.recentOrders = []
				} finally {
					this.loading = false
				}
			},
			
			goCreateOrder() {
				uni.navigateTo({
					url: '/pages/order/create'
				})
			},
			
			goOrderList() {
				uni.switchTab({
					url: '/pages/order/list'
				})
			},
			
			goProfile() {
				uni.switchTab({
					url: '/pages/user/profile'
				})
			},
			
			goEarnings() {
				uni.navigateTo({
					url: '/pages/tech/earnings'
				})
			},
			
			goOrderDetail(id) {
				uni.navigateTo({
					url: `/pages/order/detail?id=${id}`
				})
			},
			
			goRoleDashboard() {
				let url = ''
				if (this.$store.getters.isAdmin) {
					url = '/pages/admin/dashboard'
				} else if (this.$store.getters.isDispatcher) {
					url = '/pages/dispatcher/dashboard'
				} else if (this.$store.getters.isTech) {
					url = '/pages/tech/dashboard'
				}
				
				if (url) {
					uni.navigateTo({ url })
				}
			},
			
			getStatusClass(status) {
				const statusMap = {
					1: 'status-pending',
					2: 'status-processing',
					3: 'status-reviewing',
					4: 'status-completed',
					5: 'status-cancelled'
				}
				return statusMap[status] || ''
			},
			
			getStatusText(status) {
				const statusMap = {
					1: '待接单',
					2: '进行中',
					3: '待验收',
					4: '已完成',
					5: '已取消'
				}
				return statusMap[status] || '未知'
			},
			
			formatTime(time) {
				if (!time) return ''
				const date = new Date(time)
				const now = new Date()
				const diff = now - date
				
				if (diff < 60000) return '刚刚'
				if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
				if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
				return Math.floor(diff / 86400000) + '天前'
			},
			
			goLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home-container {
		padding: 20rpx;
		background: #f5f5f5;
		min-height: 100vh;
	}
	

	
	.not-login {
		display: flex;
		align-items: center;
		justify-content: center;
		min-height: 80vh;
		
		.welcome-card {
			background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
			border-radius: 20rpx;
			padding: 80rpx 60rpx;
			text-align: center;
			color: #fff;
			box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.2);
			
			.welcome-title {
				display: block;
				font-size: 48rpx;
				font-weight: bold;
				margin-bottom: 20rpx;
			}
			
			.welcome-subtitle {
				display: block;
				font-size: 28rpx;
				opacity: 0.8;
				margin-bottom: 60rpx;
			}
			
			.login-btn {
				width: 200rpx;
				height: 80rpx;
				background: rgba(255, 255, 255, 0.2);
				color: #fff;
				border: 2rpx solid rgba(255, 255, 255, 0.3);
				border-radius: 40rpx;
				font-size: 28rpx;
			}
		}
	}
	
	.user-card {
		background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
		border-radius: 20rpx;
		padding: 40rpx;
		margin-bottom: 30rpx;
		color: #fff;
		
		.user-info {
			display: flex;
			align-items: center;
			margin-bottom: 40rpx;
			
			.avatar {
				width: 100rpx;
				height: 100rpx;
				border-radius: 50rpx;
				background: rgba(255, 255, 255, 0.2);
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 30rpx;
				
				.avatar-text {
					font-size: 36rpx;
					font-weight: bold;
				}
			}
			
			.info {
				flex: 1;
				
				.name {
					display: block;
					font-size: 36rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
				}
				
				.role {
					font-size: 24rpx;
					opacity: 0.8;
				}
			}
		}
		
		.stats {
			display: flex;
			justify-content: space-around;
			
			.stat-item {
				text-align: center;
				
				.number {
					display: block;
					font-size: 48rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
				}
				
				.label {
					font-size: 24rpx;
					opacity: 0.8;
				}
			}
		}
	}
	
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		
		.more {
			font-size: 24rpx;
			color: #007AFF;
			font-weight: normal;
		}
	}
	
	.quick-actions {
		margin-bottom: 40rpx;
		
		.action-grid {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
			justify-content: space-between;
			
			.action-item {
				flex: 0 0 calc(50% - 10rpx);
				background: #fff;
				border-radius: 20rpx;
				padding: 40rpx 20rpx;
				text-align: center;
				box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
				transition: transform 0.3s ease, box-shadow 0.3s ease;
				
				&:active {
					transform: scale(0.95);
					box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.2);
				}
				
				.action-icon {
					width: 80rpx;
					height: 80rpx;
					border-radius: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin: 0 auto 20rpx;
					font-size: 36rpx;
					
					&.create {
						background: linear-gradient(45deg, #FF6B6B, #FF8E53);
						color: #fff;
					}
					
					&.orders {
						background: linear-gradient(45deg, #4ECDC4, #44A08D);
						color: #fff;
					}
					
					&.profile {
						background: linear-gradient(45deg, #A8EDEA, #FED6E3);
						color: #333;
					}
					
					&.earnings {
						background: linear-gradient(45deg, #FFD89B, #19547B);
						color: #fff;
					}
				}
				
				.action-text {
					font-size: 24rpx;
					color: #666;
				}
			}
		}
	}
	
	.recent-orders {
		margin-bottom: 40rpx;
		
		.order-list {
			.order-item {
				background: #fff;
				border-radius: 15rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;
				box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
				
				.order-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 20rpx;
					
					.order-title {
						flex: 1;
						font-size: 28rpx;
						font-weight: bold;
						color: #333;
						margin-right: 20rpx;
					}
					
					.order-status {
						font-size: 24rpx;
						padding: 4rpx 12rpx;
						border-radius: 12rpx;
					}
				}
				
				.order-info {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 15rpx;
					
					.budget {
						font-size: 32rpx;
						font-weight: bold;
						color: #FF6B6B;
					}
					
					.category {
						font-size: 24rpx;
						color: #666;
						background: #f0f0f0;
						padding: 4rpx 12rpx;
						border-radius: 12rpx;
					}
				}
				
				.order-time {
					font-size: 24rpx;
					color: #999;
				}
			}
			
			.empty {
				text-align: center;
				padding: 80rpx 0;
				color: #999;
				font-size: 28rpx;
			}
		}
	}
	
	.role-entrance {
		.entrance-item {
			background: #fff;
			border-radius: 15rpx;
			padding: 30rpx;
			display: flex;
			align-items: center;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
			
			.entrance-icon {
				width: 80rpx;
				height: 80rpx;
				border-radius: 40rpx;
				background: linear-gradient(45deg, #667eea, #764ba2);
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 30rpx;
				font-size: 36rpx;
				color: #fff;
			}
			
			.entrance-info {
				flex: 1;
				
				.entrance-title {
					display: block;
					font-size: 28rpx;
					font-weight: bold;
					color: #333;
					margin-bottom: 10rpx;
				}
				
				.entrance-desc {
					font-size: 24rpx;
					color: #666;
				}
			}
			
			.entrance-arrow {
				font-size: 32rpx;
				color: #ccc;
			}
		}
	}
</style>